package com.android.volley.toolbox;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;

import java.io.UnsupportedEncodingException;

/**
 * Created by huyaonan on 16/5/9.
 */
public class FileRequest extends Request<String> {

    private String fileTitle="";
    private boolean canUseSDCard = true;

    public FileRequest(String url, Response.Listener<String> listener, Response.ProgressListener progressListener) {
        super(Request.Method.GET, url, listener);
        setProgressListener(progressListener);
        fileTitle = url.hashCode() + "";
    }

    public void setFileTitle(String title) {
        this.fileTitle = title;
    }

    public void setCanUseSDCard(boolean canUseSDCard) {
        this.canUseSDCard = canUseSDCard;
    }

    public boolean canUseSDCard() {
        return canUseSDCard;
    }

    public String getFileTitle() {
        return fileTitle;
    }

    @Override
    public void deliverResponse(String response) {
        if(mListener != null)
            mListener.onResponse(response);
    }

    public void deliverProgress(float progress) {
        if(mProgressListener != null)
            mProgressListener.onProgress(progress);
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String parsed;
        try {
            parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        } catch (UnsupportedEncodingException e) {
            parsed = new String(response.data);
        }
        return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
    }

}
